﻿///////////////////////////////////////////////////////////////
// Projet serveur entreprise
// Authors: Pierre Corsini, Guillaume Blanc
// mars 2011
///////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Web;
using InterfaceConseiller.ServiceConseillerReference;
using System.Collections;
namespace InterfaceConseiller
{
    public partial class WinFormConseiller : Form
    {
        private ServiceConseillerReference.WebServiceConseillerSoapClient service;

        private String user, pwd;
        public WinFormConseiller(string user, string pwd)
        {
            this.user = user;
            this.pwd = pwd;

            InitializeComponent();

            service = new ServiceConseillerReference.WebServiceConseillerSoapClient();

            LoadClients();
        }

        private ServiceConseillerReference.Client currentClient = null;
        private ServiceConseillerReference.Compte currentAccount = null;
        private ArrayList client_cache = new ArrayList();
        private ArrayList compte_cache = new ArrayList();

        private void LoadClients()
        {
            userlist.Items.Clear();
            foreach (Client c in service.listeClient(user, pwd))
            {
                userlist.Items.Add(c.StringRepresentation);
                client_cache.Add(c);
            }
        }

        private void SelectClient(Client client)
        {
            LoadCompte(client);
            FillClientInfo(client);
        }

        private void FillClientInfo(Client client)
        {
            userbox.Text = client.User;
            pwdbox.Text = client.Pwd;
            prenombox.Text = client.Prenom;
            nombox.Text = client.Nom;
        }

        private void LoadCompte(Client client)
        {

            comptelistwpf.Items.Clear();
            foreach (Compte c in service.listeCompte(user, pwd, client.Id))
            {
                comptelistwpf.Items.Add(c.StringRepresentation);
                compte_cache.Add(c);
            }

        }

        private void SelectCompte(Compte compte)
        {
            this.solde.Text = compte.Solde.ToString();
            this.libelle.Text = compte.Libelle;

        }




        private void userlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.userlist.SelectedIndex < 0)
                return;

            this.currentClient = client_cache[this.userlist.SelectedIndex] as Client;
            if (this.currentClient != null)
                this.SelectClient(this.currentClient);
        }

        private void comptelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.currentAccount = compte_cache[this.comptelistwpf.SelectedIndex] as Compte;
            if (this.currentClient != null)
                this.SelectCompte(this.currentAccount);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void reloaduser_Click(object sender, EventArgs e)
        {

        }


        private void saveuser_Click(object sender, EventArgs e)
        {
            if (this.currentClient != null)
            {
                Client c = new Client();
                c.Id = currentClient.Id;
                c.User = userbox.Text;
                c.Pwd = pwdbox.Text;
                c.Nom = nombox.Text;
                c.Prenom = prenombox.Text;
                if (service.modifierInfoClient(user, pwd, c))
                    this.FillClientInfo(c);
            }

        }

        private void valeurAction_Click(object sender, EventArgs e)
        {
            if (this.currentAccount != null)
            {
                service.sommerCompte(user, pwd, this.currentAccount.Id, Double.Parse(this.valeur.Text));

                this.LoadCompte(this.currentClient);
                this.SelectCompte(this.currentAccount);
            }
        }

        private void historiquelist_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void supprimerAccount_Click(object sender, EventArgs e)
        {
            service.supprimerCompte(user, pwd, currentAccount.Id);
        }


    }
}
